package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhangyu
 * @data 2023/4/24 9:46
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info(">>>>>>>>>>> {}",BaseContext.getCurrentId());
        //拿到每个用户专属
        List<ShoppingCart> shoopingCartByUserId = shoppingCartMapper.getShoopingCartByUserId(shoppingCart);
        if (shoopingCartByUserId != null && shoopingCartByUserId.size() > 0) {
            shoppingCart = shoopingCartByUserId.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateShoppingCartCount(shoppingCart.getNumber(), shoppingCart.getId());
        } else {
            Dish dishById = dishMapper.getDishById(shoppingCartDTO.getDishId());
            if (dishById != null) {
                shoppingCart.setName(dishById.getName());
                shoppingCart.setImage(dishById.getImage());
                shoppingCart.setAmount(dishById.getPrice());
            } else {
                Setmeal setMealById = setmealMapper.getSetMealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setMealById.getName());
                shoppingCart.setImage(setMealById.getImage());
                shoppingCart.setAmount(setMealById.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insertShoppingCart(shoppingCart);


        }
    }

    @Override
    public List<ShoppingCart> selectShoppingCart() {
        return shoppingCartMapper.getShoopingCartByUserId(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoopingCartByUserId = shoppingCartMapper.getShoopingCartByUserId(shoppingCart);
        if(shoopingCartByUserId!=null&&shoopingCartByUserId.size()>0){
            shoppingCart=shoopingCartByUserId.get(0);
            Long id = shoppingCart.getId();
            if(shoppingCart.getNumber()==1){
                shoppingCartMapper.deleteOneById(id);
            }else {
              shoppingCartMapper.updateShoppingCartCount(shoppingCart.getNumber()-1,id);
            }
        }
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteShoppingByUserId(BaseContext.getCurrentId());
    }
}
